layui.define(['jquery', 'globalConfig'], function (exports) {
    let $ = layui.jquery;
    let globalConfig = layui.globalConfig;

    layui.use(["sdDialog", "sdLocal"], function () {
        let sdDialog = layui.sdDialog;
        let sdLocal = layui.sdLocal;

        // ajax请求头
        let header = {
            "Content-Type": "application/json;charset=UTF-8",
            "token": (typeof sdLocal.token.get()) == 'undefined' ? '' : sdLocal.token.get()
        };
        let sdHttp = {
            get: function (request, id) {
                request.method = 'get';
                id ? request.url = request.url + "/" + id : "";
                send(request);
                return false;
            },
            post: function (request) {
                request.method = 'post';
                send(request);
                return false;
            },
            delete: function (request, id) {
                request.method = 'delete';
                id ? request.url = request.url + "/" + id : "";
                send(request);
                return false;
            },
            put: function (request, id) {
                request.method = 'put';
                id ? request.url = request.url + "/" + id : "";
                send(request);
                return false;
            },
            info: function (request) {
                request.method = 'info';
                send(request);
                return false;
            }
        }

        function send(request) {
            let url = globalConfig.action + request.url
            if (request.url.indexOf('.json') != -1) {
                url = window.location + request.url;
            }
            header.token = (typeof sdLocal.token.get()) == 'undefined' ? '' : sdLocal.token.get();
            if (request.token) {
                header.token = request.token;
            }
       
            if((typeof request.loading) != 'undefined'){
                if(request.loading){
                    sdDialog.loading.show();
                }    
            }else{
                sdDialog.loading.show();
            }
            
            
            $.ajax({
                type: request.method,
                url: url,
                data: ajaxEmpty(request.data),
                async: typeof(request.async) == 'undefined' ? 'true' : request.async,
                contentType: "application/json;charset=UTF-8",
                sdLocal: false,
                headers: header,
                dataType: 'json',
                beforeSend: function (xhr) {
                    if (typeof(request.cl) == 'undefined') {
                    }
                },
                statusCode: {
                    200: function () {
                        sdDialog.loading.hide();
                    }
                },
                error: function (e) {
                    sdDialog.loading.hide();
                    let res = e.responseJSON;
                    let status = e.status;
                    
                    let errMessage = "服务器开小差了";
                    try{
                        let errors = res.errors;
                        errMessage = errors[0].defaultMessage;
                    }catch(e){
                        try{
                            errMessage = res.msg || "服务器开小差了";
                        }catch(ex){}
                    }
                    console.log('e', e)
                    switch (status) {
                        case 401:
                            sdDialog.tips.error(errMessage);
                            setTimeout(() => {
                                location.replace('/login.html');
                            }, 3000);
                            sdLocal.clean();
                            break;
                        case 403:
                            sdDialog.tips.warning('您没有权限进行该操作！');
                            break;
                        case 406:
                            sdDialog.tips.error(errMessage);
                            /*setTimeout(sys.exit(), 3000);*/
                            break;
                        case 411:
                            sdDialog.tips.error(errMessage);
                            /* setTimeout(sys.exit(), 3000);*/
                            break;
                        default:
                            try {
                                sdDialog.tips.error(errMessage);
                            } catch (e) {
                                sdDialog.tips.error('服务器异常通讯失败');
                            }
                    }
                    request.error ? request.error() : '';
                },
                success: function(d){
                    if(request.tips){
                        sdDialog.tips.ok(d.msg);
                    }
                    if(request.isAll){
                        request.callback ? request.callback(d) : '';
                    }else{
                        let result = d.data ? d.data : d.rows;
                        request.callback ? request.callback(result) : '';
                    }
                }
            });
        }

        function ajaxEmpty(value) {
            if (typeof(value) == "undefined")
                return "";
            if (typeof(value) == "object")  
                return JSON.stringify(value);
            return value;  
        }

        exports('sdHttp', sdHttp);    
    })

});